﻿function XMLLoadHandler(e:Event):void {
	e.target.removeEventListener(Event.COMPLETE, XMLLoadHandler);
	dataStorage.database = new XML(e.target.data);
	sharedLibs.portfolio = XML(dataStorage.database.portfolio);
	sharedLibs.about = XML(dataStorage.database.about);
	
	pageContents.push(Portfolio);
	pageContents[0].name = '0';
	pageContents.push(Sobre);
	pageContents[1].name = '1';
	pageContents.push(Contato);
	pageContents[2].name = '2';
	
	init();
}

function init():void {
	//Set containers and display elements:
	mainContainer = new MovieClip();
	mainContainer.alpha = 0;
	addChild(mainContainer);
	
	topContainer = new TopContainer();
	topContainer.x = 0;
	topContainer.y = 0;
	
	footer = new Footer();
	footer.x = 0;
	
	wallpaper = new Wallpaper();
	wallpaper.x = 0;
	wallpaper.y = topContainer.height + 2;
	mainContainer.addChild(wallpaper);
	footer.y = wallpaper.y + wallpaper.height;
	
	quotesContainer = new MovieClip();
	quotesContainer.mouseEnabled = false;
	quotesContainer.x = 851;
	quotesContainer.y = 88;
	var quoteBlur:BlurFilter = new BlurFilter();
	quoteBlur.quality = 3;
	quoteBlur.blurX = 30;
	quoteBlur.blurY = 30;
	quotesBG = new MovieClip();
	quotesBG.graphics.beginFill(0xEEEEEE);
	quotesBG.graphics.drawEllipse(0, 0, 400, 249);
	quotesBG.graphics.endFill();
	quotesBG.filters = [quoteBlur];
	quotesContainer.addChild(quotesBG);
	quotesTxt(0);
	mainContainer.addChild(quotesContainer);
	
	var contactHeaderFormat:TextFormat = new TextFormat();
	contactHeaderFormat.size = 12;
	contactHeaderFormat.color = 0x333333;
	contactHeaderFormat.align = 'center';
	contactHeaderFormat.font = 'Arial';
	var contactHeader:TextField = new TextField();
	contactHeader.defaultTextFormat = contactHeaderFormat;
	contactHeader.text = "Contato, campos marcados com \"*\" são obrigatórios:";
	contactHeader.x = 901;
	contactHeader.y = 370;
	contactHeader.autoSize = TextFieldAutoSize.LEFT;
	mainContainer.addChild(contactHeader);
	
	var formLabels:Array = new Array("Nome", "E-mail", "Telefone", "Assunto", "Mensagem");
	var inputTypes:Array = new Array('text', 'text', 'number', 'text', 'textArea');
	formmail = new FormMail(formLabels, inputTypes, formmailAction, new FormSendButton());
	formmail.requiredFields = ["Nome", "E-mail", "Mensagem"];
	formmail.x = 901;
	formmail.y = 390;
	formmail.failDialog = new FailWarning();
	formmail.successDialog = new SuccessWarning();
	mainContainer.addChild(formmail);
	
	pageContainer = new PageContainer();
	pageContainer.x = 0;
	pageContainer.y = -pageContainer.height + topContainer.height;
	dataStorage.pageContainer_initY = pageContainer.y;
	mainContainer.addChild(pageContainer);
	
	menuContainer = new MenuContainer();
	menuContainer.x = 0;
	menuContainer.y = topContainer.height + 2;
	menuContainer.name = "menuContainer";
	
	mainContainer.addChild(topContainer);
	mainContainer.addChild(footer);
	mainContainer.addChild(menuContainer);
	/////
	
	//Initialize and adds menu:
	menu_btIcons = new Array(new MenuBT_home_RGB(),
								  new MenuBT_portfolio_RGB(),
								  new MenuBT_sobre_RGB(),
								  new MenuBT_contato_RGB());
	
	menuStyles = new Object();
	menuStyles.menuPadding = 62;
	
	menu = new GWDMenu(menuStyles, menu_btIcons);
	menu.x = (menuContainer.width - menu.width) / 2 - ((9/100) * menuContainer.width);
	menuContainer.addChild(menu);
	/////
	
	dataStorage.scale = 1.0;
	dataStorage.extDataLoaded = 0;
	dataStorage.currentQuote = 0;
	
	//Set functions for buttons inside top:
	var topInner:MovieClip = MovieClip(topContainer.getChildAt(0));
	for(var currentBt:int = 0; currentBt < topInner.numChildren; currentBt++){
		if(currentBt > 1 && currentBt < 9){
			var currentElement = topInner.getChildAt(currentBt);
			currentElement.buttonMode = true;
			currentElement.addEventListener(MouseEvent.MOUSE_OVER, btTopOverHandler);
			currentElement.addEventListener(MouseEvent.CLICK, btTopClickHandler);
			
			//language buttons:
			if(currentBt == 7){
				currentElement.buttonMode = false;
				currentElement.removeEventListener(MouseEvent.CLICK, btTopClickHandler);
			}else if(currentBt == 8){
				currentElement.gotoAndStop(2);
				currentElement.removeEventListener(MouseEvent.CLICK, btTopClickHandler);
			}
		}
	}
	/////
	
	//Show site:
	dataStorage.showSite = new Tween(mainContainer, 'alpha', None.easeNone, mainContainer.alpha, 1, 1, true);
	dataStorage.showSite.addEventListener(TweenEvent.MOTION_FINISH, setSiteReady);
	
	function setSiteReady(e:TweenEvent):void {
		pageContainer.alpha = 0;
		hideMenu();
		var navDialog:NavDialog = new NavDialog();
		navDialog.x = 16;
		navDialog.y = 335;
		navDialog.name = 'navDialog';
		mainContainer.addChild(navDialog);
		var currentPage:String = String(dataStorage.flashVars.currentPage);
		sharedLibs.flashVars = dataStorage.flashVars;
		switch(currentPage){
			case 'portfolio':
				switchPage('1');
				break;
			case 'curriculo':
				switchPage('2');
				break;
			case 'contato':
				switchPage('3');
				break;
			default:
				if(String(dataStorage.flashVars.mailed) == 'true'){
					formmail.showWarning(true);
				}
				break;
		}
		setInterval(changeQuote, 10000);
	}
	/////
}

function changeQuote():void {
	if(dataStorage.currentQuote < (dataStorage.database.famousQuotes.quote.length() - 1)){
		dataStorage.currentQuote++;
	} else {
		dataStorage.currentQuote = 0;
	}
	var quoteOut:Tween = new Tween(quotesTxtField, 'alpha', Strong.easeIn, 1, 0, 0.5, true);
	quoteOut.addEventListener(TweenEvent.MOTION_FINISH, outMotionHandler);
	function outMotionHandler(ev:TweenEvent):void {
		quotesTxt(dataStorage.currentQuote);
		quotesTxtField.alpha = 0;
		new Tween(quotesTxtField, 'alpha', Strong.easeOut, 0, 1, 0.5, true);
	}
}

function quotesTxt(currentQuote:int):void {
	/////Text format:
	quotesTxtFormat = new TextFormat();
	quotesTxtFormat.font = 'Arial';
	quotesTxtFormat.color = 0x333333;
	quotesTxtFormat.size = 16;
	quotesTxtFormat.align = 'center';
	quotesTxtFormat.italic = true;
	quotesTxtFormat.bold = true;
	/////
	
	/////Text field:
	quotesTxtField = new TextField();
	quotesTxtField.width = 300;
	quotesTxtField.selectable = true;
	quotesTxtField.wordWrap = true;
	quotesTxtField.autoSize = TextFieldAutoSize.CENTER;
	quotesTxtField.defaultTextFormat = quotesTxtFormat;
	quotesTxtField.text = '\"' + String(dataStorage.database.famousQuotes.quote[currentQuote].text) + '\"' + "\n\n - " + String(dataStorage.database.famousQuotes.quote[currentQuote].author);
	quotesTxtField.x = 50;
	quotesTxtField.y = (quotesBG.height - quotesTxtField.height) / 2;
	quotesContainer.addChild(quotesTxtField);
	/////
}

function btTopClickHandler(e:MouseEvent):void{
	var request:URLRequest;
	var window:String = "_blank";
	
	switch(e.currentTarget.name){
		case 'Facebook':
			request = new URLRequest("http://www.facebook.com");
			break;
		case 'btGoogle':
			request = new URLRequest("http://www.google.com.br");
			break;
		case 'Twitter':
			request = new URLRequest("http://www.twitter.com");
			break;
		case 'LinkedIn':
			request = new URLRequest("http://www.linkedin.com");
			break;
		case 'Blog':
			request = new URLRequest("http://www.blogger.com");
			break;
		case 'pt_BR':
			request = new URLRequest("http://www.guelficonsultor.com.br/adminsoft");
			window = null;
			break;
		case 'en_UK':
			request = new URLRequest("http://www.guelficonsultor.com.br/adminsoft");
			window = null;
			break;
	}
	navigateToURL(request, window);
}

function btTopOverHandler(e:MouseEvent):void{
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_OVER, btTopOverHandler);
	e.currentTarget.addEventListener(MouseEvent.MOUSE_OUT, btTopOutHandler);
	
	var topDialog:Top_Dialog = new Top_Dialog();
	topDialog.x = mouseX;
	topDialog.y = mouseY + 20;
	topDialog.mouseEnabled = false;
	topDialog.name = "topDialog";
	addChild(topDialog);
	
	var labelFormat:TextFormat = new TextFormat();
	labelFormat.size = 16;
	labelFormat.color = 0xffffff;
	labelFormat.font = "Arial";
	labelFormat.align = "center";
	
	var dialogLabel:TextField = new TextField();
	dialogLabel.text = (e.currentTarget.name == "btGoogle" ? "Google+" : e.currentTarget.name);
	dialogLabel.width = topDialog.width;
	dialogLabel.selectable = false;
	dialogLabel.mouseEnabled = false;
	dialogLabel.setTextFormat(labelFormat);
	topDialog.addChild(dialogLabel);
}

function btTopOutHandler(e:MouseEvent):void{
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_OUT, btTopOutHandler);
	e.currentTarget.addEventListener(MouseEvent.MOUSE_OVER, btTopOverHandler);
	removeChild(getChildByName("topDialog"));
}

function menuOverHandler(e:MouseEvent):void {
	showMenu();
}

function showMenu():void {
	var navDialog:DisplayObject = mainContainer.getChildByName('navDialog');
	navDialog.alpha = 0;
	menuContainer.removeEventListener(MouseEvent.MOUSE_OVER, menuOverHandler);
	var show_menu:Tween = new Tween(menuContainer, 'x', Strong.easeOut, menuContainer.x, 0, 0.5, true);
	show_menu.addEventListener(TweenEvent.MOTION_FINISH, motionHandler);
	function motionHandler(ev:TweenEvent) {
		if(isMouseOverArea(menuContainer)){
			menu.btRollOverEffects(btOverHandler);
			menu.btClickEffects(btClickHandler);
			menuContainer.addEventListener(MouseEvent.MOUSE_OUT, menuOutHandler);
			
			var bts:Array = menu.buttons;
			for(var btIndex:int = 0; btIndex < bts.length; btIndex++) {
				var btArea:Object = new Object();
				btArea.x = (menuContainer.width - menu.width) / 2 - ((9/100) * menuContainer.width);
				btArea.y = (bts[btIndex].height + menuStyles.menuPadding) * btIndex + menuStyles.menuPadding + topContainer.height;
				btArea.width = bts[btIndex].width;
				btArea.height = bts[btIndex].height;
				if(Number(bts[btIndex].name) != dataStorage.extDataLoaded){
					bts[btIndex].buttonMode = true;
					if(isMouseOverArea(btArea)) {
						btOverFunc(bts[btIndex]);
					}
				}
			}
		}else {
			hideMenu();
		}
	}
}

function menuOutHandler(e:MouseEvent):void {
	if(!isMouseOverArea(menuContainer)) {
		hideMenu();
	}
}

function hideMenu(switchingPage:Boolean = false):void {
	menuContainer.removeEventListener(MouseEvent.MOUSE_OUT, menuOutHandler);
	menu.btRollOverEffects(btOverHandler, true);
	menu.btClickEffects(btClickHandler, true);
	var bts:Array = menu.buttons;
	for(var btIndex:int = 0; btIndex < bts.length; btIndex++){
		bts[btIndex].buttonMode = false;
		if(bts[btIndex].numChildren > 1){
			btOutFunc(bts[btIndex]);
		}
	}
	
	dataStorage.hideMenu = new Tween(menuContainer, 'x', Strong.easeOut, 0, -160, 0.7, true);
	dataStorage.hideMenu.addEventListener(TweenEvent.MOTION_FINISH, motionHandler);
	function motionHandler(ev:TweenEvent):void {
		var btPbIcons:Array = new Array(new MenuBT_home_PB(),
								  new MenuBT_portfolio_PB(),
								  new MenuBT_sobre_PB(),
								  new MenuBT_contato_PB());
		menu.buttons = btPbIcons;
		if(!switchingPage){
			menuContainer.addEventListener(MouseEvent.MOUSE_OVER, menuOverHandler);
			if(isMouseOverArea(menuContainer)){
				showMenu();
			}
		}
	}
}

function btClickHandler(e:MouseEvent):void {
	if(Number(e.currentTarget.name) != dataStorage.extDataLoaded){
		hideMenu(true);
		switchPage(e.currentTarget.name);
	}
}

function switchPage(pageName:String):void {
	dataStorage.extDataLoaded = Number(pageName);
	if(pageContainer.y > dataStorage.pageContainer_initY){
		hidePage(pageName);
	}else{
		if(pageName != '0') {
			pageName = String(Number(pageName) - 1);
			for(var pageIndex:int = 0; pageIndex < pageContents.length; pageIndex++){
				if(pageContents[pageIndex].name == pageName){
					var tmpPage:MovieClip = new pageContents[pageIndex];
					tmpPage.name = 'tmpPage';
					tmpPage.preStart(sharedLibs);
					pageContainer.addChildAt(tmpPage, 1);
				}
			}
			var show_page:Tween = new Tween(pageContainer, 'y', Strong.easeOut, pageContainer.y, topContainer.height, 1, true);
			new Tween(pageContainer, 'alpha', Strong.easeOut, 0, 1, 0.5, true);
			show_page.addEventListener(TweenEvent.MOTION_FINISH, motionHandler);
			
			function motionHandler(ev:TweenEvent):void {
				tmpPage.start();
				sharedLibs.flashVars.currentPage = '';
				menuContainer.addEventListener(MouseEvent.MOUSE_OVER, menuOverHandler);
				if(isMouseOverArea(menuContainer)){
					showMenu();
				}
			}
		}else{
			menuContainer.addEventListener(MouseEvent.MOUSE_OVER, menuOverHandler);
			if(isMouseOverArea(menuContainer)){
				showMenu();
			}
		}
	}
}

function hidePage(pageName:String = 'no'):void {
	var hide_page:Tween = new Tween(pageContainer, 'y', Strong.easeOut, pageContainer.y, -pageContainer.height + topContainer.height, 0.5, true);
	new Tween(pageContainer, 'alpha', Strong.easeOut, pageContainer.alpha, 0, 0.5, true);
	hide_page.addEventListener(TweenEvent.MOTION_FINISH, motionHandler);
	
	function motionHandler(ev:TweenEvent):void {
		var currentPage:DisplayObject = pageContainer.getChildByName('tmpPage');
		if(pageContainer.contains(currentPage)){
			pageContainer.removeChild(currentPage);
		}
		if(pageName != 'no'){
			switchPage(pageName);
		}
	}
}

function btOverHandler(e:MouseEvent):void {
	if(Number(e.currentTarget.name) != dataStorage.extDataLoaded){
		btOverFunc(e.currentTarget);
	}
}

function btOverFunc(target:Object):void {
	target.removeEventListener(MouseEvent.MOUSE_OVER, btOverHandler);
	target.addEventListener(MouseEvent.MOUSE_OUT, btOutHandler);
	
	var overStatus:MovieClip;
	
	var menuDialog:MenuDialog = new MenuDialog();
	menuDialog.x = menuContainer.width;
	menuDialog.y = target.y + (target.height - menuDialog.height) / 2 + topContainer.height;
	menuDialog.mouseEnabled = false;
	menuDialog.name = "menuDialog";
	addChild(menuDialog);
	
	var labelFormat:TextFormat = new TextFormat();
	labelFormat.size = 20;
	labelFormat.color = 0xffffff;
	labelFormat.font = "Arial";
	labelFormat.align = "center";
	
	var dialogLabel:TextField = new TextField();
	dialogLabel.width = menuDialog.width;
	dialogLabel.selectable = false;
	dialogLabel.mouseEnabled = false;
	dialogLabel.defaultTextFormat = labelFormat;
	menuDialog.addChild(dialogLabel);
	
	switch(target.name){
		case '0':
			overStatus = new MenuBT_home_RGB();
			dialogLabel.text = "  Home Page";
			break;
		case '1':
			overStatus = new MenuBT_portfolio_RGB();
			dialogLabel.text = "Portfolio";
			break;
		case '2':
			overStatus = new MenuBT_sobre_RGB();
			dialogLabel.text = "Sobre";
			break;
		case '3':
			overStatus = new MenuBT_contato_RGB();
			dialogLabel.text = "Contato";
			break;
	}
	overStatus.mouseEnabled = false;
	target.addChild(overStatus);
}

function btOutHandler(e:MouseEvent):void {
	e.currentTarget.addEventListener(MouseEvent.MOUSE_OVER, btOverHandler);
	btOutFunc(e.currentTarget);
}

function btOutFunc(target:Object):void {
	target.removeEventListener(MouseEvent.MOUSE_OUT, btOutHandler);
	removeChild(getChildByName("menuDialog"));
	target.removeChildAt(target.numChildren - 1);
}

function formmailAction(requestArguments:Object):void {
	requestArguments.formmail = 'true';
	
	navigateToURL(new URLRequest(URIQueryString('index.php', requestArguments)), '_self');
}

function isMouseOverArea(target:Object):Boolean {
	var answer:Boolean = false;

	if(mouseX >= target.x && mouseX <= (target.width + target.x)){
		if(mouseY >= target.y && mouseY <= (target.height + target.y)){
			answer = true;
		}
	}
	return answer;
}

function URIQueryString(initURL:String, vars:Object):String {
	var finalURI:String = initURL;
	var queryString:String = "?";
	
	for(var key:String in vars){
		queryString += strStandards(key) + "=" + String(vars[key] + "&");
	}
	queryString = queryString.substring(0, queryString.length - 1);
	finalURI += queryString;
	
	return finalURI;
}

function strStandards(str:String):String{
	var arrPatterns:Array = new Array();
	
	arrPatterns.push( { pattern:/[äáàâãª]/g,  char:'a' } );
	arrPatterns.push( { pattern:/[ÄÁÀÂÃ]/g,  char:'A' } );
	arrPatterns.push( { pattern:/[ëéèê]/g,   char:'e' } );
	arrPatterns.push( { pattern:/[ËÉÈÊ]/g,   char:'E' } );
	arrPatterns.push( { pattern:/[íîïì]/g,   char:'i' } );
	arrPatterns.push( { pattern:/[ÍÎÏÌ]/g,   char:'I' } );
	arrPatterns.push( { pattern:/[öóòôõº]/g,  char:'o' } );
	arrPatterns.push( { pattern:/[ÖÓÒÔÕ]/g,  char:'O' } );
	arrPatterns.push( { pattern:/[üúùû]/g,   char:'u' } );
	arrPatterns.push( { pattern:/[ÜÚÙÛ]/g,   char:'U' } );
	arrPatterns.push( { pattern:/[ç]/g,   char:'c' } );
	arrPatterns.push( { pattern:/[Ç]/g,   char:'C' } );
	arrPatterns.push( { pattern:/[ñ]/g,   char:'n' } );
	arrPatterns.push( { pattern:/[Ñ]/g,   char:'N' } );
	arrPatterns.push( { pattern:/[\- ]/g,   char:'' } );
	
	for(var i:uint = 0; i < arrPatterns.length; i++){
		str = str.replace(arrPatterns[i].pattern, arrPatterns[i].char);
	}
 
	return str;
}